package com.tenc.job.task;

import com.tenc.feign.InitService;
import com.tenc.utils.RedisUtil;
import com.tenc.pojo.ErrorJson;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
public class WechatAccessTokenTask implements Job {
    @Value("wechat.company.corpid")
    private String corpid;
    @Value("wechat.company.corpsecret")
    private String corpsecret;

    @Autowired
    InitService initService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            log.info("执行定时任务：{}",jobExecutionContext.getScheduler().getSchedulerName());
            ErrorJson accessToken = initService.getAccessToken(corpid, corpsecret);
            if(accessToken.getErrcode()!=0){
                log.info("access_token获取失败\n错误原因：{}",accessToken);
            }else{
                RedisUtil.set("acToken",accessToken.getAccess_token());
            }
        } catch (SchedulerException e) {
            log.info("执行定时任务：{}",e.getMessage());
            e.printStackTrace();
        }

    }
}
